package com.example.orderfood2.viewAllInformation;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.R;
import com.example.orderfood2.RecyclerViewAdapter.ClassAdapter;
import com.example.orderfood2.Repository.ClassInfoRepository;
import com.example.orderfood2.util.DBUtil;

import androidx.recyclerview.widget.LinearLayoutManager;

public class ViewAllClassInformationActivity extends AppCompatActivity implements ClassAdapter.OnItemClickListener {
    private ClassInfoRepository dbUtil;
    Cursor cursor;
    private RecyclerView recyclerClassesView;
    private ClassAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        创建数据库
//        dbUtil = new DBUtil(this);
        dbUtil= DatabaseManager.getInstance(this).getClassInfoRepository();
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_view_all_class_information);
        // 初始化RecyclerView
        recyclerClassesView = findViewById(R.id.recyclerClassesView);
//      作用是确定 RecyclerView 中各个列表项的排列方式为以线性列表的形式展示数据
        recyclerClassesView.setLayoutManager(new LinearLayoutManager(this));
        // 加载数据
        loadClassData();
    }

    private void loadClassData() {
        cursor = dbUtil.getAllClasses();
        adapter = new ClassAdapter(cursor);
        adapter.setOnItemClickListener(this); // 设置按钮点击监听器
        recyclerClassesView.setAdapter(adapter);
    }


    @Override
    protected void onDestroy() {
        if (cursor != null) {
            cursor.close();
        }
        dbUtil.close();
        super.onDestroy();
    }

    // 修改按钮点击事件
    @Override
    public void onEditClick(int position, int id, String classNumber) {
        Intent intent = new Intent(this, EditClassActivity.class); // 跳转到编辑页面
        intent.putExtra("id", id);
        startActivity(intent);
    }

    // 删除按钮点击事件
    @Override
    public void onDeleteClick(int position, int id) {
        new AlertDialog.Builder(this)
                .setTitle("确认删除")
                .setMessage("确定删除此班级吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    dbUtil.deleteClass(id); // 从数据库删除
                    loadClassData(); // 刷新列表
                })
                .setNegativeButton("取消", null)
                .show();
    }

    // 接收编辑页面的返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            loadClassData(); // 刷新列表
        }
    }
}